package org.lwjgl.demo.device;
import org.lwjgl.BufferUtils;

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import static org.lwjgl.opengl.GL30.*;
public class TextureLoader {
    
    /**
     * 加载纹理文件 - 修复版本
     */
    public static int loadTexture(String filePath) {
        // ==================== 修改：简化文件检查，专注于生成纹理 ====================
        System.out.println("加载纹理: " + filePath);
        
        int textureId = glGenTextures();
        glBindTexture(GL_TEXTURE_2D, textureId);
        
        // 设置纹理参数
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        
        // ==================== 修改：直接生成木箱纹理，不依赖外部文件 ====================
        ByteBuffer imageData = generateWoodenCrateTexture(256, 256);
        
        // 上传纹理数据
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
        glGenerateMipmap(GL_TEXTURE_2D);
        
        glBindTexture(GL_TEXTURE_2D, 0);
        System.out.println("✅ 木箱纹理生成成功 (256x256)");
        return textureId;
    }
    
    /**
     * 生成木箱纹理
     */
    private static ByteBuffer generateWoodenCrateTexture(int width, int height) {
        ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 3);
        java.util.Random random = new java.util.Random(42);
        
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                // 基础木色 - 深棕色
                float baseR = 0.55f;  // 深棕色R
                float baseG = 0.27f;  // 深棕色G  
                float baseB = 0.07f;  // 深棕色B
                
                // 木纹效果
                float woodGrain = (float) Math.sin(x * 0.02f + y * 0.01f) * 0.15f;
                woodGrain += (float) Math.sin(x * 0.05f) * 0.08f;
                woodGrain += (float) Math.sin(y * 0.03f) * 0.05f;
                
                // 随机噪点
                float noise = (random.nextFloat() - 0.5f) * 0.1f;
                
                // 木板接缝
                boolean isSeam = (x % 64 < 2) || (y % 64 < 2);
                if (isSeam) {
                    // 接缝颜色 - 深色
                    baseR *= 0.6f;
                    baseG *= 0.6f;
                    baseB *= 0.6f;
                }
                
                // 计算最终颜色
                float r = Math.max(0, Math.min(1, baseR + woodGrain + noise));
                float g = Math.max(0, Math.min(1, baseG + woodGrain * 0.7f + noise));
                float b = Math.max(0, Math.min(1, baseB + woodGrain * 0.4f + noise));
                
                buffer.put((byte) (r * 255));
                buffer.put((byte) (g * 255));
                buffer.put((byte) (b * 255));
            }
        }
        buffer.flip();
        return buffer;
    }
    
    /**
     * 创建纯色纹理
     */
    public static int createSolidColorTexture(int r, int g, int b) {
        int textureId = glGenTextures();
        glBindTexture(GL_TEXTURE_2D, textureId);
        
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        
        ByteBuffer imageData = BufferUtils.createByteBuffer(3);
        imageData.put((byte) r).put((byte) g).put((byte) b);
        imageData.flip();
        
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
        glBindTexture(GL_TEXTURE_2D, 0);
        
        return textureId;
    }
    
    /**
     * 创建球体纹理 - 蓝色大理石效果
     */
    public static int createSphereTexture(int width, int height) {
        int textureId = glGenTextures();
        glBindTexture(GL_TEXTURE_2D, textureId);
        
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        
        ByteBuffer imageData = generateSphereTexture(width, height);
        
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
        glGenerateMipmap(GL_TEXTURE_2D);
        
        glBindTexture(GL_TEXTURE_2D, 0);
        System.out.println("✅ 球体纹理生成成功");
        return textureId;
    }
    
    /**
     * 生成球体纹理 - 蓝色大理石效果
     */
    private static ByteBuffer generateSphereTexture(int width, int height) {
        ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 3);
        java.util.Random random = new java.util.Random(123);
        
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                // 基础蓝色
                float baseR = 0.2f;
                float baseG = 0.4f;
                float baseB = 0.8f;
                
                // 大理石纹理效果
                float noise = (float) Math.sin(x * 0.1f) * (float) Math.cos(y * 0.1f) * 0.3f;
                noise += (float) Math.sin(x * 0.05f + y * 0.03f) * 0.2f;
                noise += (random.nextFloat() - 0.5f) * 0.1f;
                
                float r = Math.max(0, Math.min(1, baseR + noise));
                float g = Math.max(0, Math.min(1, baseG + noise * 0.8f));
                float b = Math.max(0, Math.min(1, baseB + noise * 0.5f));
                
                buffer.put((byte) (r * 255));
                buffer.put((byte) (g * 255));
                buffer.put((byte) (b * 255));
            }
        }
        buffer.flip();
        return buffer;
    }

    /**
     * 创建专门的镜面反射贴图
     */
    public static int createSpecularMap(int width, int height) {
        int textureId = glGenTextures();
        glBindTexture(GL_TEXTURE_2D, textureId);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        // 创建高光贴图 - 边缘亮，中间暗
        ByteBuffer imageData = generateSpecularPattern(width, height);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
        glGenerateMipmap(GL_TEXTURE_2D);

        glBindTexture(GL_TEXTURE_2D, 0);
        return textureId;
    }

    /**
     * 生成高光图案 - 边框高光效果
     */
    private static ByteBuffer generateSpecularPattern(int width, int height) {
        ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 3);

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                // 计算到边缘的距离
                float distToEdgeX = Math.min(x, width - x) / (float) width;
                float distToEdgeY = Math.min(y, height - y) / (float) height;
                float distToEdge = Math.min(distToEdgeX, distToEdgeY);

                // 边缘区域高光更强
                float specularStrength;
                if (distToEdge < 0.1f) {
                    specularStrength = 1.0f; // 边框最强高光
                } else if (distToEdge < 0.2f) {
                    specularStrength = 0.7f; // 次边框中等高光
                } else {
                    specularStrength = 0.3f; // 中心区域较弱高光
                }

                byte value = (byte) (specularStrength * 255);
                buffer.put(value); // R
                buffer.put(value); // G
                buffer.put(value); // B
            }
        }
        buffer.flip();
        return buffer;
    }
}